package com.lagou.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName RabbitConfig
 * @Description TODO
 * @Author xsq
 * @Date 2020/11/12 16:54
 **/
@Configuration
public class RabbitConfig {
    public static String queueName = "queue.shopping";
    public static String exchangeName = "ex.shopping";
    public static String dlxExchangeOrderName = "dlx.ex.shopping";
    public static String routeOrderName = "order.shopping";
    public  static String dlxRouteOrderName = "dlx.order.shopping";
    public final static String dlxQueueOrderName = "dlx.queue.shopping";

    /**
     * 声明普通队列
     *
     * @return
     */
    @Bean
    public Queue queue() {
        Map<String, Object> props = new HashMap<>();
        props.put("x-message-ttl", 20000);
        props.put("x-dead-letter-exchange", dlxExchangeOrderName);
        props.put("x-dead-letter-routing-key", dlxRouteOrderName);
        return new Queue(queueName, false, false, false, props);
    }

    @Bean
    public Exchange exchange() {
        return new DirectExchange(exchangeName, false, false, null);
    }


    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(exchange()).with(routeOrderName).noargs();
    }

    /**
     * 声明死信队列
     *
     * @return
     */
    @Bean
    public Queue queueDlx() {
        Queue queue = new Queue(dlxQueueOrderName, true, false, false);
        return queue;
    }

    @Bean
    public Exchange exchangeDlx() {
        return new DirectExchange(dlxExchangeOrderName, false, false, null);
    }

    @Bean
    public Binding dlxBinding() {
        return BindingBuilder.bind(queueDlx()).to(exchangeDlx()).with(dlxRouteOrderName).noargs();
    }

}
